#include <stdio.h>
#include "mpi.h"
#include "oom.h"

int
main(int argc, char **argv)
{
	int size, rank, ranks[2], my_newgroup_rank;
	MPI_Group worldgroup, newgroup;

	MPI_Init(&argc, &argv);
	
	MPI_Comm_size(MPI_COMM_WORLD, &size);
	MPI_Comm_rank(MPI_COMM_WORLD, &rank);
	
	MPI_Comm_group(MPI_COMM_WORLD, &worldgroup);

	ranks[0] = 1;
	ranks[1] = 0;

	MPI_Group_incl(worldgroup, 2, ranks, &newgroup);
	MPI_Group_rank(newgroup, &my_newgroup_rank);

	if (rank == 0)
	{
		if (my_newgroup_rank == 1)
			printf("world rank 0 in rank 1 of new group\n");
		else
			printf("ERROR: world rank 0 rank in new group incorrect\n"); 
	}
	else if (rank == 1)
	{
		if (my_newgroup_rank == 0)
			printf("world rank 1 in rank 0 of new group\n");
		else
			printf("ERROR: world rank 1 rank in new group incorrect\n"); 
	}
	else if (my_newgroup_rank != MPI_UNDEFINED)
	{
		printf("ERROR: world rank other than 0 and 1 in new group\n");
	}
	else {
		printf("world rank %d not in new group\n", rank);
	}

	MPI_Finalize();
	return 0;
}

